package com.fingard.dsp.bank.directbank.boc03;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
/**
 * 境外他行 余额查询
 * @return
 * @throws Exception
 */
public class BOC03Balance extends BOC03Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        return null;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            BOC03Header bankHeader = new BOC03Header();
            bankHeader.trncod = "b2e0586";//交易代码
            bankHeader.trnid = getQryTransID();//报文编号 字母数字串 0-12位
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader2(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("trn-b2e0586-rq");
            xmlWriter.writeStartElement("b2e0586-rq");
            xmlWriter.writeElementString("fromdate", p_req7505.dateFrom);
            xmlWriter.writeElementString("enddate", p_req7505.dateTo);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString());

            //测试
            //String[] tmpRetBkStr=new String[]{"",FileHelper.readFileText("D:\\zjbr-接口测试\\25.中行联行号下载\\境外账户查询\\rsp\\b2e0586.xml","utf-8")};

            if (tmpRetBkStr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmprspcod = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0586-rs/status/rspcod");
                    String tmprspmsg = tmpBkReader.getFirstText("/bocb2e/trans/trn-b2e0586-rs/status/rspmsg");
                    if(isActiveRetCode(tmprspcod)){
                        while (tmpBkReader.read("/bocb2e/trans/trn-b2e0586-rs/b2e0586-rs")){
                            String rspcod = tmpBkReader.readedXElement.getSingleText("status/rspcod");
                            String rspmsg = tmpBkReader.readedXElement.getSingleText("status/rspmsg");
                            if ("B001".equals(rspcod)) {
                                String actNum=tmpBkReader.readedXElement.getSingleText("detail/account/actnum").trim();
                                if(p_req7505.actNum.equals(actNum)){
                                    int childNum=tmpBkReader.readedXElement.getSingleChild("detail").sizeSubNode();//子节点个数
                                    if(childNum>0){
                                        for (int i=1;i<childNum;i++){
                                            RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                                            String dateTime=tmpBkReader.readedXElement.getSingleText("detail/balanceinfo"+i+"/transtime"+i);
                                            if(dateTime.length()>10){
                                                dateTime=dateTime.substring(0,10);
                                            }
                                            if(dateTime.length()==10){
                                                dateTime=dateTime.replaceAll("/","-");
                                            }
                                            tmpRetDetail.balDate =dateTime;
                                            tmpRetDetail.actBal = tmpBkReader.readedXElement.getSingleText("detail/balanceinfo"+i+"/balance"+i);
                                            retAts.writeRetDetail(tmpRetDetail);
                                        }
                                    }
                                }
                            }else {
                                tmpRespCode = "-1";
                                tmpRespInfo = rspmsg;
                            }
                        }
                    }else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmprspmsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpRetBkStr[1];
            }

        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }
}
